<?php
namespace Common\Controller;
class BaseController extends \Think\Controller{
    protected $data = '';


    /**
     * 检测登录
     */

    function check_login($success = '', $scope = '')
    {
        if (!$_SESSION['openid'] || !$_SESSION['userid']) {
            //检测用户来源入口
            $pk_id = I('pk_id');
            $this -> assign('pk_id',$pk_id);



            if (!$success) $current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            $WeiXin = new \Weixin\Controller\WeixinFuwuController();
            $WeiXin->oauth2_run($current_url, 'info',$_GET['way']);
        }
    }



    /**
     * 检测登录
     */
//    protected function check_login($success_url=''){
//        if($_SESSION['userid'] == 52 && !$_SESSION['tscl']){
//            unset($_SESSION['openid']);
//            $_SESSION['tscl'] = 1;
//        }
//        $_SESSION['openid'] = 'ovma9vkanZ75V-FPAvZLon0LO9x4';
//        $_SESSION['userid'] = 52;
//        $_SESSION['nickname'] = '叶叶惜惜';
//        if(!$_SESSION['openid'] || !$_SESSION['userid'] || !$_SESSION['nickname']){
//            $success_url = $success_url ? $success_url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
//            $callback_url = urlencode(C('APP_URL').U('Home/Index/login').'?success_url='.$success_url);
//            $way = $_GET['way'];
//            die(header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('APPID').'&redirect_uri='.$callback_url.'&response_type=code&scope=snsapi_userinfo&state='.$way.'#wechat_redirect'));
//        }
//    }
//
//
    /**
     * 获取用户信息
     */
//    protected function oauth2_user_info($code){
//        $result = curl_post('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APPID').'&secret='.C('APP_SECRET').'&code='.$code.'&grant_type=authorization_code');
//        $result = json_decode($result,true);
//        $uInfo =  curl_post('https://api.weixin.qq.com/sns/userinfo?access_token='.$result['access_token'].'&openid='.$result['openid'].'&lang=zh_CN');
//        $uInfo = json_decode($uInfo,true);
//        if($uInfo['errcode'] == 0){
//            return $uInfo;
//        }else{
//            die($uInfo['errmsg']);
//        }
//    }



    /**
     * 返回结果
     * @param type $erorr 错误码
     * @param type $msg 错误描述
     * @param type $model 模型
     */
    protected function rtnResult($erorr,$msg,$model=array()){
        $this->data['error'] = $erorr;
        $this->data['msg'] = $msg;
        if(!empty($model)){
            $erorr == 0 ? $model->commit() : $model->rollback();
        }
        die;
    }



    /**
     * 显示视图
     * @param string $data 要赋值的数据
     * @param string $file 输出的模板文件名
     */
    protected function showView($data='',$file=''){
//        $data['wxKey'] = $this->rtnQM();
        //模板输出
        if (!empty($data) && is_array($data)){
            foreach ($data as $key => $value){
                $this->assign($key,$value);
            }
        }
        $this->display($file);
    }


    /**
     * 获取当前可用次数
     * @return int|mixed
     */
    function getPlayNumber(){
        !$_SESSION['userid'] && die('not found uid');
        $uid = $_SESSION['userid'];


        $up_time = S('clean_time');
        if(empty($up_time)){
            S('clean_time',date('Y-m-d'));
        }else{
            $nowtime = date('Y-m-d');
            if($nowtime != $up_time){ //不是当天
                //清空次数缓存
                S('number_'.$uid,null);
                //用户数据表中未用完的次数清零
                $user = M('user')->where('pk_id=%d',$_SESSION['userid'])->setField('play_times',0);
                //将三次重新赋给
//                S('number_'.$uid,3);
                S('clean_time',date('Y-m-d'));
            }

        }


        $play = S('number_'.$uid);
        if(!$play){

            $cur_date = strtotime(date('Y-m-d'));
            //取出今天玩的次数
            $nowtime = time();
            if($nowtime>= $cur_date){

                $play = M('play')->where('fk_user_id=%d',$_SESSION['userid'])->count();
                $play = C('FREE_NUM') - $play;
                S('number_'.$uid,$play);
            }

        }
//        dump($play);




        //数据库中的可用次数统计

        $play_time = M('user')->where('pk_id=%d',$_SESSION['userid'])->getField('play_times');
        if(!$play_time || $play_time<=0){
            $play_time =0;
        }
//        dump($play);dump($play_time);
        //将免费次数和数据库中邀请好友获得的次数加在一起，为当天拥有的总次数
        $play =$play + $play_time;
        // $play < 0 && $play = 0;
//        dump($play);
//        dump($play_time);
//        die;
        return $play;

    }


    /**
     * 降低可用次数
     */
    function decNumber($number){
//        $number = S('number_'.$_SESSION['userid']);
//        $number = $this->getPlayNumber();
        //先使用免费的次数，再用数据库里面的
        $play = S('number_'.$_SESSION['userid']);
        if($play > 0){
            S('number_'.$_SESSION['userid'] , $number-1);
        }else{
            //将数据库里面的次数减一


            $play_time = M('user')->where('pk_id=%d',$_SESSION['userid'])->getField('play_times');
            if(!$play_time || $play_time<=0){
                //如果数据库中次数为0，则不减少一次


            } else{
//                dump(12345);
                $play = M('user')->where('pk_id=%d',$_SESSION['userid'])->setDec('play_times',1);
            }



        }

    }


    function __destruct() {
        if(!empty($this->data)){
            $this->ajaxReturn($this->data);
        }
    }
    
}

